<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>输入用户号</title>
</head>
<link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">

<link rel="stylesheet" href="../../src/dist/css/page.css">

<body>
<div class="weui-cells userquery-page">
    <div class="weui-cells__tips">用户信息页</div>
    <div class="weui-cell">
        <div class="weui-cell__hd">
            <i class="iconfont iconjiage"></i>
        </div>
        <div class="weui-cell__bd">
            <p>燃气费</p>
        </div>
        <div class="weui-cell__ft"></div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd">
            <i class="iconfont iconzhanghao"></i>
        </div>
        <div class="weui-cell__bd">
            <p>用户号</p>
        </div>

        <div class="weui-cell__bd">
            <input class="weui-input" type="number" pattern="[0-9]*" placeholder="请输入用户账号" id="usercode">
        </div>
    </div>
    <div class="button_sp_area">
        <a href="javascript:;" class="weui-btn weui-btn_primary" onclick="goNextStep()">下一步</a>
    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>

<!-- Dist -->
<script src="../../src/dist/js/index.js"></script>
<script src="../../src/dist/js/utils.js"></script>
<script src="../../src/dist/js/request.js"></script>
<script>
    function goNextStep(){
      let usercode = $("#usercode").val()
      if(usercode === ''){
        $.toast("用户号不能为空", "text")
        return
      }
        var reg = /^\d{10}$/
        if (!reg.test(usercode)) {
			$.toast("用户号格式不正确", "text");
            return false
        }

        // 获取用户信息
        get("/gasuser/custom/getTransUserByUsercode?usercode="+ usercode, {}, function (data) {
        	//alert(JSON.stringify(data))
            if (data.code === 0) {
                // 使用模版引擎加载数据
                let userDataList = data.data
                console.log(userDataList)
                if (userDataList === null || userDataList.length === 0) {
				    $.toast('该缴费户号不存在!', 'forbidden');
                } else if (userDataList.length === 1) {
					// 跳转到信息展示与对应的缴费页面
					// 判断气价是否失效，如果是物联网表判断是否已经表命令同步，判断后付费还是预付费
					let userData = userDataList[0]
					if (userData.scrapped !== null && userData.scrapped === '1') { // 气价已弃用
					    $.toast('用户气价已失效，请去营业厅办理!', 'forbidden');
					    return false
					}
					if (userData.meterid === null) {
                        $.toast('没有找到用户对应燃气表具，不能充值!', 'forbidden');
					    return false
					}

					// 进行表具校验(如果是真兰物联网表查询上次充值是否已到账)
                    get("/gasuser/custom/checkMeter?meterid=" + userData.meterid, {}, function (data) {
                        if (data.code === 0) {
                            if (userData.metertype !== null && userData.metertype === '1') {
                            	// 基表 后付费
							    window.location.href = 'payment.html?usercode=' + usercode
                            } else { // 其他表具 预付费
								window.location.href = 'prepayment.html?usercode=' + usercode
                            }
                        } else {
                            $.toast(data.msg, 'forbidden');
                        }
                    }, false, true);
                }
            } else {
                $.toast('获取用户数据失败!', 'forbidden');
            }
        }, false, true);
    }
</script>
</body>
</html>
